﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

public partial class IsLogion : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            if (object.Equals(Session["UserName"], null))
            {
            }
            else  // 如果登陆，就直接转到购物车
            {
                InsertProduct();
                Response.Redirect("shoppingCar.aspx");
            }
        }
   }
    // 登录（将购物车中的物品显示）
    protected void imgBtnLogin_Click(object sender, ImageClickEventArgs e)
    {
        try
        {
            BLL.Business.UserBLL userBLL = new BLL.Business.UserBLL();

            String UserName = this.username.Text.ToString();
            String Password = this.password.Text.ToString();

            if (userBLL.LoginSucess(UserName, Password))    // 登陆成功转到购物车
            {
                // -------------------  添加物品到购物车START  ------------------
                Session["UserName"] = UserName;
                bool IsBuyNow = Int32.Parse(Request.QueryString["buyNow"].ToString()) > 0 ? true : false;  // 判断是否是立即购买
                int buyCount = Int32.Parse(Request.QueryString["buyCount"].ToString());
                if (!object.Equals(Request.QueryString["Book_Idint"], null))
                {
                    String req_bookId = Request.QueryString["Book_Idint"];                                 // 获得传进来的产品号码

                    if (IsBuyNow)// 立即购买
                    {
                        if (!object.Equals(Session["orderList"], null))
                        {
                            Session.Remove("orderList");
                        }
                        Session["orderList"] = new Hashtable();
                        ((Hashtable)Session["orderList"]).Add(req_bookId, 1);
                        Response.Redirect("oderCheck.aspx");
                    }
                    else         // 添加购物车
                    {
                        InsertProduct();
                        Response.Redirect("shoppingCar.aspx");
                    }
                }
                // -------------------  添加物品到购物车END  ---------------------
            }
            else
            {
                Response.Write("<script>alert('密码不正确或是用户名不存在！请重新输入')</script>");
            }
        }
        catch (Exception ex)
        {
            Response.Write("<script>alert('" + ex.Message.ToString() + "')</script>");
        }
    }
    public void InsertProduct()
    {
        BLL.Business.ShoppingCarBLL carBLL = new BLL.Business.ShoppingCarBLL();
        BLL.Business.UserBLL userBLL = new BLL.Business.UserBLL();
        Model.ShoppingCar car = new Model.ShoppingCar();
        try
        {
            if ((!object.Equals(Session["UserName"], null)) &&
                (!object.Equals(Request.QueryString["Book_Idint"], null)))
            {
                String userName = Session["UserName"].ToString();       // 目前登陆的用户
                String req_bookId = Request.QueryString["Book_Idint"];  // 获得传进来的产品号码
                int userId = 0;
                int bookId = 0;
                int count = 0;

                DataSet ds = userBLL.SelectByName(userName);
                if ((ds == null) ||
                   (ds.Tables.Count == 0) ||
                   (ds.Tables.Count == 1 && ds.Tables[0].Rows.Count == 0))
                {
                }
                else
                {
                    userId = Int32.Parse(ds.Tables[0].Rows[0]["User_Idint"].ToString());
                    bookId = Int32.Parse(req_bookId);                 // 商品编号
                    count = 1;                                        // 商品数量

                    car.User_Idint = userId;
                    car.Book_Idint = bookId;
                    car.Car_Quantityint = count;
                    if (carBLL.Insert(car))
                    {
                    }
                    else
                    {
                        Response.Write("<script>alert('添加购物车出现问题！')</script>");
                    }
                }
            }
        }
        catch (Exception ex)
        {
            Response.Write("<script>alert('" + ex.Message.ToString() + "')</script>");
        }
    }
}
